using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;

namespace Client
{
    public class BaseRepository<T>
    {
        public string Login    { get; set; }
        public string Password { get; set; }

        public BaseRepository()
        {
            Login    = String.Empty;
            Password = String.Empty;
        }

        public BaseRepository(string login, string password)
        {
            Login    = login;
            Password = password;
        }

        public IEnumerable<T> ParseJson(string jsonArray)
        {
            try
            {
                List<T> resultList              = new List<T>();
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                jsonArray = jsonArray.Trim('[', ']').Replace("},{", "}" + Defaults.DEFAULT_DELIMETER + "{");

                foreach (string obj in jsonArray.Split(';'))
                {
                    resultList.Add(serializer.Deserialize<T>(obj));
                }

                return resultList.AsEnumerable<T>();
            }
            catch { }

            return null;
        }
    }

    public class PositionRepository : BaseRepository<Position>, IPositionRepository
    {
        private const string ADD_REQ     = Defaults.SERVER + @"m/add_position/?login=#login#&password=#password#&name=#name#&salary=#salary#";
        private const string GET_ALL_REQ = Defaults.SERVER + @"m/positions/?login=#login#&password=#password#";

        public PositionRepository() : base()
        {
        }

        public PositionRepository(string login, string password) : base(login, password)
        {
        }

        public bool Add(Position position)
        {
            if (position != null)
            {
                HttpWebResponse response = Client.GetResponse(ADD_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password).Replace(@"#salary#", position.Salary.ToString()).Replace(@"#name#", position.Name),
                                                              "POST",
                                                              Defaults.DEFAULT_CONTENTTYPE,
                                                              null);

                if (response != null)
                {
                    response.Close();
                    return true;
                }
            }
            return false;
        }

        public IEnumerable<Position> GetAll()
        {
            HttpWebResponse response = Client.GetResponse(GET_ALL_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password),
                                                          "POST",
                                                          Defaults.DEFAULT_CONTENTTYPE,
                                                          null);

            if (response != null)
            {
                StreamReader responseReader = new StreamReader(response.GetResponseStream());
                string       jsonResult     = responseReader.ReadToEnd();

                response.Close();
                return ParseJson(jsonResult);
            }
            return null;
        }
    }

    public class BankAccountRepository : BaseRepository<BankAccount>, IBankAccountRepository
    { 
        public BankAccountRepository() : base()
        {
        }

        public BankAccountRepository(string login, string password) : base(login, password)
        {
        }
    }

    public class PaymentRepository : BaseRepository<Payment>, IPaymentRepository
    {
        public PaymentRepository(string login, string password) : base(login, password)
        {
        }

        public PaymentRepository() : base()
        {
        }
    }

    public class TaskRepository : BaseRepository<Task>, ITaskRepository
    {
        public TaskRepository() : base()
        {
        }

        public TaskRepository(string login, string password) : base(login, password)
        {
        }
    }

    public class TimeTableRepository : BaseRepository<TimeTable>, ITimeTableRepository
    {
        private const string LOGIN_REQ  = Defaults.SERVER + @"login/?login=#login#&password=#password#";
        private const string LOGOUT_REQ = Defaults.SERVER + @"logout/?login=#login#&password=#password#";

        public TimeTableRepository() : base()
        {
        }
        
        public TimeTableRepository(string login, string password) : base(login, password)
        {
        }

        public bool LogIn()
        {
            HttpWebResponse response = Client.GetResponse(LOGIN_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password),
                                                          "POST",
                                                          Defaults.DEFAULT_CONTENTTYPE,
                                                          null);

            if (response != null)
            {
                response.Close();
                return true;
            }
            return false;
        }

        public bool LogOut()
        {
            HttpWebResponse response = Client.GetResponse(LOGOUT_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password),
                                                          "POST",
                                                          Defaults.DEFAULT_CONTENTTYPE,
                                                          null);

            if (response != null)
            {
                response.Close();
                return true;
            }
            return false;
        }
    }

    public class EmployeeRepository : BaseRepository<Employee>, IEmployeeRepository
    {
        private const string GET_EMPLOYEE_REQ = Defaults.SERVER + @"employee_data/?login=#login#&password=#password#";
        private const string ADD_EMPLOYEE_REQ = Defaults.SERVER + @"m/add_employee/?login=#login#&password=#password#&name=#name#&bank_account=#bankAccountId#&position=#positionId#&role=#role#&e_login=#e_login#&e_password=#e_password#";

        public EmployeeRepository()  : base()
        {
        }

        public EmployeeRepository(string login, string password) : base(login, password)
        {
        }

        public Employee GetData()
        {
            HttpWebResponse response = Client.GetResponse(GET_EMPLOYEE_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password),
                                                          "POST",
                                                          Defaults.DEFAULT_CONTENTTYPE,
                                                          null);

            if (response != null)
            {
                StreamReader responseReader = new StreamReader(response.GetResponseStream());
                string       jsonResult     = responseReader.ReadToEnd();

                response.Close();
                return ParseJson(jsonResult).First();
            }
            return null;
        }

        public bool Add(Employee employee)
        {
            HttpWebResponse response = Client.GetResponse(ADD_EMPLOYEE_REQ.Replace(@"#login#", Login).Replace(@"#password#", Password).Replace(@"#name#", employee.Name).Replace(@"#bankAccountId#", employee.BankAccountId.ToString()).Replace(@"#positionId#", employee.PositionId.ToString()).Replace(@"#role#", employee.Role.ToString()).Replace(@"#e_login#"
                , employee.Login).Replace(@"#e_password", employee.Password),
                                                          "POST",
                                                          Defaults.DEFAULT_CONTENTTYPE,
                                                          null);

            if (response != null)
            {
                response.Close();
                
                return true;
            }
            return false;
        }
    }
}
